package com.test.dababy.dababy_02_permission;

import android.annotation.SuppressLint;
import android.content.Context;

import com.test.dababy.R;
import com.test.dababy.dababy_01_launcher.Base_dababyApplication;

import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;


public class PermissionAdapter extends RecyclerView.Adapter<PermissionAdapter.PermissionHolder> {

    private int t47E=175;
    private float d85L=0.52709675f;
    private String p1k3="";
    private String niuE="";
    private String x70L="";
    private String l5kH="";
    private String eh1B="";
    private double fmY7=0.369964047282072;
    private float ahsZ=0.47001076f;

    public void bFR9V(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getP1k3()))
                    setNiuE(getX70L()+getT47E()+getD85L());
                else setL5kH(getEh1B()+getFmY7()+getAhsZ());

            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setT47E(int t47E) {
        this.t47E = t47E;
    }

    public void setD85L(float d85L) {
        this.d85L = d85L;
    }

    public void setP1k3(String p1k3) {
        this.p1k3 = p1k3;
    }

    public void setNiuE(String niuE) {
        this.niuE = niuE;
    }

    public void setX70L(String x70L) {
        this.x70L = x70L;
    }

    public void setL5kH(String l5kH) {
        this.l5kH = l5kH;
    }

    public void setEh1B(String eh1B) {
        this.eh1B = eh1B;
    }

    public void setFmY7(double fmY7) {
        this.fmY7 = fmY7;
    }

    public void setAhsZ(float ahsZ) {
        this.ahsZ = ahsZ;
    }

    public int getT47E() {
        return t47E;
    }

    public float getD85L() {
        return d85L;
    }

    public String getP1k3() {
        return p1k3;
    }

    public String getNiuE() {
        return niuE;
    }

    public String getX70L() {
        return x70L;
    }

    public String getL5kH() {
        return l5kH;
    }

    public String getEh1B() {
        return eh1B;
    }

    public double getFmY7() {
        return fmY7;
    }

    public float getAhsZ() {
        return ahsZ;
    }

    private final Context context;

    private final ArrayList<PermissionInfo> infos;

    public PermissionAdapter(Context context, ArrayList<PermissionInfo> infos){
        this.context = context;
        this.infos = infos;
    }

    public PermissionInfo getInfo(int index){
        return infos.get(index);
    }

    public boolean isPermissionHasHandle(int index){
        if (index<0 || index>=infos.size()) return false;
        PermissionInfo info = infos.get(index);
        if (info == null || info.getHolder() == null) return false;
        return info.getHolder().getStatus() != PermissionStatus.Default_dababy;
    }

    @NonNull
    @Override
    public PermissionHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PermissionHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_dababy_permission,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull PermissionHolder holder, int position) {
        infos.get(position).setHolder(holder);
    }

    @Override
    public int getItemCount() {
        bFR9V();
        return infos.size();
    }

    public static class PermissionHolder extends RecyclerView.ViewHolder{

        private final ImageView imgIv;
        private final TextView titleTv;
        private final TextView descTv;
        private final View readView,btnView,bannedVeiw,authorizedView;
        private final ImageButton readIb;
        private final TextView denyBtn,agreeBtn;

        private boolean isCheck = false;
        private PermissionStatus status = PermissionStatus.Default_dababy;

        @SuppressLint("InflateParams")
        public PermissionHolder(View view){

            super(view);
            
            imgIv = view.findViewById(R.id.vdp_iv_img);
            titleTv = view.findViewById(R.id.vdp_tv_title);
            descTv = view.findViewById(R.id.vdp_tv_desc);
            readView = view.findViewById(R.id.vdp_view_read);
            btnView = view.findViewById(R.id.vdp_view_btn);
            bannedVeiw = view.findViewById(R.id.vdp_view_banned);
            authorizedView = view.findViewById(R.id.vdp_view_authorized);
            readIb = view.findViewById(R.id.vdp_ib_read);
            denyBtn = view.findViewById(R.id.vdp_btn_deny);
            agreeBtn = view.findViewById(R.id.vdp_btn_agree);

            readIb.setOnClickListener(v -> setCheck(!isCheck));
        }

        public void setImg(int drawableId){
            imgIv.setBackgroundResource(drawableId);
        }

        public void setTitle(CharSequence title){
            titleTv.setText(title);
        }

        public void setDesc(String desc){
            descTv.setText(Html.fromHtml(desc,0));
        }

        public void setCheck(boolean isCheck){
            this.isCheck = isCheck;
            readIb.setBackgroundResource(isCheck ? R.drawable.icon_check_true:R.drawable.icon_check_false);
        }

        public boolean isCheck() {
            return isCheck;
        }

        public void setPermissionStatus(PermissionStatus status) {
            this.status = status;
            switch (status){
                case Deny_dababy:
                    btnView.setVisibility(View.GONE);
                    readView.setVisibility(View.GONE);
                    authorizedView.setVisibility(View.GONE);
                    bannedVeiw.setVisibility(View.VISIBLE);
                    break;
                case Agree_dababy:
                    btnView.setVisibility(View.GONE);
                    readView.setVisibility(View.GONE);
                    authorizedView.setVisibility(View.VISIBLE);
                    bannedVeiw.setVisibility(View.GONE);
                    break;
                case Default_dababy:
                    btnView.setVisibility(View.VISIBLE);
                    readView.setVisibility(View.VISIBLE);
                    authorizedView.setVisibility(View.GONE);
                    bannedVeiw.setVisibility(View.GONE);
                    break;
            }
        }

        public PermissionStatus getStatus() {
            return status;
        }

        public void setDenyClickListener(View.OnClickListener onClickListener){
            denyBtn.setOnClickListener(onClickListener);
        }

        public void setAgreeClickListener(View.OnClickListener onClickListener){
            agreeBtn.setOnClickListener(onClickListener);
        }

        public void setNextClickListener(View.OnClickListener onClickListener){
            bannedVeiw.setOnClickListener(onClickListener);
            authorizedView.setOnClickListener(onClickListener);
        }

    }
}
